import 'package:finance_app/generated/json/base/json_convert_content.dart';
import 'package:finance_app/generated/json/base/json_field.dart';
import 'package:finance_app/src/entities/business_contact_entity.dart';
import 'package:finance_app/src/entities/format/service_and_product_entity.dart';
import 'package:finance_app/src/entities/ocr_attachment_entity.dart';
import 'package:finance_app/src/entities/product_entity.dart';
import 'package:finance_app/src/entities/services_entity.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';

class BillDetailEntity with JsonConvert<BillDetailEntity> {
  @JSONField(name: "data_type")
  String dataType;
  int id;
  @JSONField(name: "company_id")
  int companyId;
  @JSONField(name: "purchase_order_id")
  int purchaseOrderId;
  @JSONField(name: "contact_id")
  int contactId;
  BusinessContactEntity contact;
  @JSONField(name: "category_id")
  int categoryId;
  SettingDataCategory category;
  String number;
  @JSONField(name: "purchase_order_number")
  String purchaseOrderNumber;
  @JSONField(name: "issued_at")
  String issuedAt;
  @JSONField(name: "due_at")
  String dueAt;
  @JSONField(name: "currency_code")
  String currencyCode;
  num subtotal;
  @JSONField(name: "exclusive_tax_amount")
  num exclusiveTaxAmount;
  @JSONField(name: "inclusive_tax_amount")
  num inclusiveTaxAmount;
  @JSONField(name: "discount_type")
  String discountType;
  @JSONField(name: "discount_amount")
  num discountAmount;
  @JSONField(name: "credit_used")
  num creditUsed;
  @JSONField(name: "discount_value")
  String discountValue;
  num total;
  List<BillDetailItems> items;
  dynamic notes;
  String status;
  @JSONField(name: "created_by")
  dynamic createdBy;
  @JSONField(name: "created_at")
  String createdAt;
  @JSONField(name: "updated_at")
  String updatedAt;
  List<OcrAttachmentEntity> attachments;

  @JSONField(name: "is_overdue")
  int isOverdue;

  @JSONField(name: "delivered_at")
  String deliveredAt;

  num unpaid;
}

class BillDetailItems with JsonConvert<BillDetailItems> {
  @JSONField(name: "data_type")
  String dataType;
  int id;
  @JSONField(name: "company_id")
  int companyId;
  @JSONField(name: "bill_id")
  int billId;
  String type;
  String name;
  int quantity;
  @JSONField(name: "currency_code")
  String currencyCode;
  @JSONField(name: "tax_id")
  int taxId;
  @JSONField(name: "type_id")
  int typeId;
  SettingDataTax tax;
  num price;
  @JSONField(name: "nett_price")
  num nettPrice;
  @JSONField(name: "is_tax_inclusive")
  int isTaxInclusive;
  @JSONField(name: "tax_amount")
  num taxAmount;
  @JSONField(name: "created_at")
  String createdAt;
  @JSONField(name: "updated_at")
  String updatedAt;
  @JSONField(name: "discount_type")
  String discountType;
  @JSONField(name: "discount_value")
  num discountValue;

  ServiceAndProductEntity toServiceAndProduct() {
    ServicesEntity servicesEntity = ServicesEntity();
    ProductEntity productEntity = ProductEntity();
    if (type == 'product') {
      productEntity
        ..id = typeId
        ..name = name
        ..isTaxInclusive = isTaxInclusive
        ..price = price
        ..currencyCode = currencyCode
        ..discountType = discountType
        ..discountValue = discountValue
        // ..tax =
        ..taxId = taxId
        ..tax = tax;
    } else {
      servicesEntity
        ..id = typeId
        ..name = name
        ..isTaxInclusive = isTaxInclusive
        ..price = price
        ..currencyCode = currencyCode
        ..discountType = discountType
        ..discountValue = discountValue
        // ..tax =
        ..taxId = taxId
        ..tax = tax;
    }
    return ServiceAndProductEntity()
      ..services = servicesEntity
      ..product = productEntity
      ..number = quantity;
  }
}
